package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.advertisment.model.AdvOverviewData;
import com.loongtech.bi.advertisment.model.chart.*;
import com.loongtech.bi.advertisment.service.AdvOverviewService;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * ClassName: AdvDayDataAction
 * 
 * @Description: TODO 市场数据分析 ——  广告投放概览
 * @author liuchen
 * @date 2017-06-15 下午2:23:49
 */
@Controller("advOverviewReportAction")
@Scope("view")
public class AdvOverviewReportAction extends BaseAdvAction {
	private static final long serialVersionUID = 1L;

	private String dateRadio = "7"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY)); // 自选时间：开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY)); // 自选时间：结束时间

	private AdvOverviewData advOverviewData;

	@Resource(name = "advOverviewService")
	private AdvOverviewService service;

	// 数据概览
	private List<ClickChartData> clickChartData; // 点击趋势数据
	private List<ActiveChartData> acitveChartData; // 激活趋势数据
	private List<NewAccountChartData> newAccountChartData; // 新增账号趋势数据
	private List<PayChartData> payChartData; // 付费金额趋势数据
	private List<ClickActiveRateData> clickActiveRateData; // 点击-激活转化率数据
	private List<ActiveNewRateData> activeNewRateData; // 激活-新增转化率数据

	// 效果数据
	private List<DiscountData> discountData; // 单位成本数据
	private List<RemainData> remainData; // 留存趋势数据
	private List<LtvData> ltvData; // LTV数据

	// TOP指标
	private List<TopClickData> topClickData; // top点击数据
	private List<TopActiveData> topActiveData; // top激活数据
	private List<TopPayData> topPayData; // top付费数据

	private List<ChartUIData> advChart; // 数据趋势图表
	private List<ChartUIData> effectChart; // 效果数据图表
	private List<ChartUIData> topChart; // TOP指标图表

	@PostConstruct
	public void init() {
		search();
	}

	public void search() {

		advChart = null;

		effectChart = null;

		topChart = null;

		FacesContext context = FacesContext.getCurrentInstance();
		if (hasAgents == null || hasAgents.size() <= 0) {
			context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
			return;
		}
		if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
			context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
			return;
		}

		try {
			String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart
					: new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY)))
					.getTime()))); // 开始时间
			String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd
					: new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()))); // 结束时间
			advOverviewData = service.getActiveTranDatas(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
            advOverviewData.setNewPay(UIHelper.getYuan(advOverviewData.getNewPay()));

			clickChartData = service.getClickChartUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);

			advChart = new ArrayList<>();
			effectChart = new ArrayList<>();
			topChart = new ArrayList<>();
			if (clickChartData != null && clickChartData.size() > 0) {
				ChartUIData payClassChart0 = new ChartUIData();
				payClassChart0 = new ChartUIData();
				payClassChart0.setJsId("0");
				payClassChart0.setJsonType(ChartType.SPLINE.type);
				payClassChart0.setJsonStr(new ChartTool(clickChartData, ClickChartData.class, "")
						.loadData(payClassChart0.getJsonType()));
				advChart.add(payClassChart0);
			}

			acitveChartData = service.getAcitveChartUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
			if (acitveChartData != null && acitveChartData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("1");
				payClassChart1.setJsonType(ChartType.SPLINE.type);
				payClassChart1.setJsonStr(new ChartTool(acitveChartData, ActiveChartData.class, "")
						.loadData(payClassChart1.getJsonType()));
				advChart.add(payClassChart1);
			}

			newAccountChartData = service.getNewAccountChartUIData(startDay, endDay, selectedChannelName,
					selectedAgent, selectedPlatform, selectedPaytype);
			if (newAccountChartData != null && newAccountChartData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("2");
				payClassChart2.setJsonType(ChartType.SPLINE.type);
				payClassChart2.setJsonStr(new ChartTool(newAccountChartData, NewAccountChartData.class, "")
						.loadData(payClassChart2.getJsonType()));
				advChart.add(payClassChart2);
			}

			payChartData = service.getPayChartUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("3");
				payClassChart3.setJsonType(ChartType.SPLINE.type);
				payClassChart3.setJsonStr(new ChartTool(payChartData, PayChartData.class, "").loadData(payClassChart3
						.getJsonType()));
				advChart.add(payClassChart3);
			}

			clickActiveRateData = service.getClickActiveRateUIData(startDay, endDay, selectedChannelName,
					selectedAgent, selectedPlatform, selectedPaytype);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart4 = new ChartUIData();
				payClassChart4 = new ChartUIData();
				payClassChart4.setJsId("4");
				payClassChart4.setJsonType(ChartType.SPLINE.type);
				payClassChart4.setJsonStr(new ChartTool(clickActiveRateData, ClickActiveRateData.class, "")
						.loadData(payClassChart4.getJsonType()));
				advChart.add(payClassChart4);
			}

			activeNewRateData = service.getActiveNewRateUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
			if (payChartData != null && payChartData.size() > 0) {
				ChartUIData payClassChart5 = new ChartUIData();
				payClassChart5 = new ChartUIData();
				payClassChart5.setJsId("5");
				payClassChart5.setJsonType(ChartType.SPLINE.type);
				payClassChart5.setJsonStr(new ChartTool(activeNewRateData, ActiveNewRateData.class, "")
						.loadData(payClassChart5.getJsonType()));
				advChart.add(payClassChart5);
			}

			/*discountData = service.getDiscountUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
			if (discountData != null && discountData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("6");
				payClassChart1.setJsonType(ChartType.SPLINE.type);
				payClassChart1.setJsonStr(new ChartTool(discountData, DiscountData.class, "").loadData(payClassChart1
						.getJsonType()));
				effectChart.add(payClassChart1);
			}*/

			remainData = service.getRemainUIData(startDay, endDay, selectedChannelName, selectedAgent,
					selectedPlatform, selectedPaytype);
			if (remainData != null && remainData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("7");
				payClassChart2.setJsonType(ChartType.SPLINE.type);
				payClassChart2.setJsonStr(new ChartTool(remainData, RemainData.class, "").loadData(payClassChart2
						.getJsonType()));
				effectChart.add(payClassChart2);
			}

			ltvData = service.getLtvUIData(startDay, endDay, selectedChannelName, selectedAgent, selectedPlatform,
					selectedPaytype);
			if (ltvData != null && ltvData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("8");
				payClassChart3.setJsonType(ChartType.SPLINE.type);
				payClassChart3.setJsonStr(new ChartTool(ltvData, LtvData.class, "").loadData(payClassChart3
						.getJsonType()));
				effectChart.add(payClassChart3);
			}

			topClickData = service.getTopClickUIData(startDay, endDay);
			if (topClickData != null && topClickData.size() > 0) {
				ChartUIData payClassChart1 = new ChartUIData();
				payClassChart1 = new ChartUIData();
				payClassChart1.setJsId("9");
				payClassChart1.setJsonType(ChartType.BAR2.type);
				payClassChart1.setJsonStr(new ChartTool(topClickData, TopClickData.class, "").loadData(payClassChart1
						.getJsonType()));
				topChart.add(payClassChart1);
			}

			topActiveData = service.getTopActiveUIData(startDay, endDay);
			if (topActiveData != null && topActiveData.size() > 0) {
				ChartUIData payClassChart2 = new ChartUIData();
				payClassChart2 = new ChartUIData();
				payClassChart2.setJsId("10");
				payClassChart2.setJsonType(ChartType.BAR2.type);
				payClassChart2.setJsonStr(new ChartTool(topActiveData, TopActiveData.class, "").loadData(payClassChart2
						.getJsonType()));
				topChart.add(payClassChart2);
			}

			topPayData = service.getTopPayUIData(startDay, endDay);
			if (topPayData != null && topPayData.size() > 0) {
				ChartUIData payClassChart3 = new ChartUIData();
				payClassChart3 = new ChartUIData();
				payClassChart3.setJsId("11");
				payClassChart3.setJsonType(ChartType.BAR2.type);
				payClassChart3.setJsonStr(new ChartTool(topPayData, TopPayData.class, "").loadData(payClassChart3
						.getJsonType()));
				topChart.add(payClassChart3);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

	public AdvOverviewData getAdvOverviewData() {
		return advOverviewData;
	}

	public void setAdvOverviewData(AdvOverviewData advOverviewData) {
		this.advOverviewData = advOverviewData;
	}

	public List<ChartUIData> getAdvChart() {
		return advChart;
	}

	public void setAdvChart(List<ChartUIData> advChart) {
		this.advChart = advChart;
	}

	public List<ChartUIData> getEffectChart() {
		return effectChart;
	}

	public void setEffectChart(List<ChartUIData> effectChart) {
		this.effectChart = effectChart;
	}

	public List<ChartUIData> getTopChart() {
		return topChart;
	}

	public void setTopChart(List<ChartUIData> topChart) {
		this.topChart = topChart;
	}

}
